class Solution {
public:
    string fractionToDecimal(int numerator, int denominator) {
        if (numerator == 0) return "0";
        bool fu = false;
        long long n = numerator, d = denominator;
        if (n < 0) n = -n, fu = !fu;
        if (d < 0) d = -d, fu = !fu;
        long long num = n % d;
        string ret = to_string(n / d);
        if (fu) ret.insert(0, "-");
        if (num == 0) return ret;
        ret += '.';

        unordered_map<long long, int> hash;

        while (num) {
            num *= 10;
            if (hash.find(num) != hash.end()) {
                ret.insert(hash[num], "(");
                ret += ')';
                return ret;
            }
            ret += to_string(num / d);
            hash[num] = ret.size() - 1;
            num = num % d;
        }
        return ret;
    }
};